﻿using Dapper.IService;
using Dapper.Model;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Dapper.Service
{
    public class UserService : IUserService
    {

        private readonly IConfiguration _configuration;
        private readonly IDBFactory _dbFactory;
        public UserService(IConfiguration configuration, IDBFactory dbFactory)
        {
            _configuration = configuration;
            _dbFactory = dbFactory;
        }
        public IDbConnection GetConnection()
        {
            var connectionString = _configuration.GetSection("ConnectionString").GetSection("mysqlServer").Value;
            var conn = new MySqlConnection(connectionString);
            return conn;
        }

        public List<User> GetUsers()
        {
            using (var conn = this.GetConnection())
            {
                return conn.Query<User>("select * from Users").AsList();
            }
        }

        public User GetUser(string userId)
        {
            //using(var conn = DBConfig.GetSqlConnection())
            //{
            //    return conn.QuerySingle<User>("select * from User where userId=" + userId);
            //}
            using (var conn = _dbFactory.Create())
            {
                return conn.QuerySingle<User>("select * from Users where userId=" + userId);
            }
        }
    }
}
